var http = require("http");
var querystring = require("querystring");

 function createServers(route) {
  var server = http.createServer(function (req, res) {
    var data = "";
    req.on("data", function (_data) {
      data += _data;
    });
    req.on("end", function () {
      if (data.trim().length === 0) data = req.url.split("?")[1];
      else {
        try {
          data = JSON.parse(data)
        } catch (error) {
     
        }
      }
      if (typeof data === "string") {
        try {
          data = querystring.parse(data);
        } catch (error) {
          res.end("错误的消息");
          return;
        }
      }
      if (!data) {
        res.end("错误的消息");
        return;
      }
      
      

      res.writeHead(200, {
        "content-type": "text/html;charset=utf-8",
        "Access-Control-Allow-Origin": "*",
      })

      switch (req.url.split("?")[0]) {
        case "/selectSql":
          route.select(res, data);
          break;



        case "/deleteSql":
          route.delete(res, data);
          break;
        case "/insertSql":
          route.insert(res, data);
          break;
        case "/updateSql":
          route.update(res, data);
          break;
      }
    })
  })
  server.listen(4008, function () {
    console.log("4008服务器启动成功");
  });
} 
module.exports = createServers;